<?php
$file = $_FILES['file'];
if(!empty($file))
{
    $work = new work($_POST['group'],$_POST['type'],$_POST['name']);
    $file_desc = $work->reArrayFiles($file);

    foreach($file_desc as $val)
    {
        $name = iconv("UTF-8", "gb2312", $work->dir."/".$val["name"]);
        move_uploaded_file($val['tmp_name'],$name);
    }
    header('Location: jump.html');
}

class work{
    /**
     * @var string 组别
     */
    public $group = "";
    /**
     * @var string 姓名
     */
    public $userName = "";
    /**
     * @var string 文件类型
     */
    public $type = "";
    /**
     * @var string 文件路径
     */
    public $dir = "";
    public function __construct($group,$type,$userName = ""){
        $this->group = $group;
        $this->type = $type;
        $this->userName = $userName;
        $this->createDir();
    }

    public function createDir(){
        $dayName = date('Y-m-d',time());
        if($this->type == "完成情况"){
            $this->dir = "homework/{$dayName}/{$this->group}";
        }else{
            $this->dir = "homework/{$dayName}/{$this->group}/{$this->type}/{$this->userName}";
        }
        $this->checkDir();
    }
    /**
     * @param $path 文件路径
     * @param $name 文件夹名
     */
    function checkDir(){
        $dirArr = explode("/",$this->dir);
        $url = "./";
        foreach($dirArr as $val){
            $url .= $val;
            if(!file_exists(iconv('utf-8', 'gbk', $url))){
                mkdir(iconv('utf-8', 'gbk', $url));
            }
            $url.="/";
        }
//        $this->dir = $url;
    }
    /**
     * @param $file
     * @return array
     * 返回指定的文件数组
     */
    function reArrayFiles($file)
    {
        $file_ary = array();
        $file_count = count($file['name']);
        $file_key = array_keys($file);
        for($i=0;$i<$file_count;$i++)
        {
            foreach($file_key as $val)
            {
                $file_ary[$i][$val] = $file[$val][$i];
            }
        }
        return $file_ary;
    }
}